import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: {},
    isItemDetailLoading: false
  },
  getters: {
    isLogin: state => !!state.user.token,
    level: state => state.user.level || -1000,
    username: state => state.user.username,
    token: state => state.user.token
  },
  mutations: {
    login: (state, {user}) => {
      state.user = user;
      axios.defaults.headers.common["Authorization"] = user.token;
      // console.log(user.token)
      localStorage.setItem('dkp-master-user', JSON.stringify(user))
    },
    logout: (state) => {
      state.user = {};
      localStorage.removeItem('dkp-master-user')
    },
    setItemDetailLoadingState: (state, {isLoading}) => {
      state.itemDetailLoading = isLoading;
    }
  }
})
